package com.scy.hospitalmanager.controller;

import com.google.gson.Gson;
import com.scy.hospitalmanager.pojo.NurseLogin;
import com.scy.hospitalmanager.pojo.Patient;
import com.scy.hospitalmanager.pojo.PatientPhysicianOrder;
import com.scy.hospitalmanager.service.impl.PatientPhysicianServiceImpl;
import com.scy.hospitalmanager.until.GsonUtil;
import com.scy.hospitalmanager.until.ResponseMap;
import com.scy.hospitalmanager.until.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Description:
 * @ClassName: PatientPhysicianController
 * @Author: KuonnjiArisu
 * @Date: 2021/8/27 16:00
 * @Version: 1.0
 */
@Controller
public class PatientPhysicianController {
    @Autowired
    PatientPhysicianServiceImpl patientPhysicianService;
    Gson gson = new Gson();

    /**
     * @return
     * @Author KuonnjiAirsu
     * @Description //TODO 通过患者id查询医嘱信息
     * @Param
     */
    @PostMapping("/getPhysicianByPatientById")
    @ResponseBody
    public ResponseMap getPhysicianByPatientById(@RequestBody Patient patient, @RequestHeader("token") String token) {

        if (patient != null) {
//            System.out.println(patient+"病人");
            TokenUtil.parsePayloadData(token);

//            System.out.println(patientPhysicianService.getPhysicianById(patient));
            return ResponseMap.OK(patientPhysicianService.getPhysicianById(patient));
        } else {
            return ResponseMap.NO();
        }


    }

    /**
     * @return
     * @Author KuonnjiAirsu
     * @Description //TODO 通过患者id填写空白的医嘱信息
     * @Param
     */
    @PostMapping("/updatePatientPhysicianOrder")
    @ResponseBody
    public ResponseMap updatePatientPhysicianOrder(@RequestBody PatientPhysicianOrder patientPhysicianOrder,@RequestHeader("token") String token) {
        if (patientPhysicianOrder != null) {
            Map<String,Object>nurseMap= TokenUtil.parsePayloadData(token);
            String str = gson.toJson(nurseMap.get("nurseLogin"));
            NurseLogin nurseLogin = gson.fromJson(str,NurseLogin.class);
//            System.out.println(nurseLogin);
            patientPhysicianOrder.setPoExecutionNurse(nurseLogin.getNurse().getNurName());
            return ResponseMap.OK(patientPhysicianService.updatePatientPhysicianOrder(patientPhysicianOrder));
        } else {
            return ResponseMap.NO();
        }

    }
}
